import jwt
from flask import current_app
from datetime import datetime,timedelta

def generate_token(payload, expiry, algorithm='HS256'):
    key = current_app.config.get('SECRET_KEY')
    if 'exp' not in payload:
        payload['exp'] = expiry
    try:
        token = jwt.encode(payload,key,algorithm)
        return token
    except Exception as e:
        print('错误>>', e)
        return e

def check_token(token):
    key = current_app.config.get('SECRET_KEY')
    payload = jwt.decode(token,key,algorithms=['HS256'])
    return payload

def _generate_token(payload, is_refresh=True):
    expiry = datetime.now() + timedelta(hours=2)
    token = generate_token(payload, expiry)
    return token